package com.fps.web.controller;


import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fps.web.model.Mminfo;
import com.fps.web.service.BaseService;
import com.fps.web.service.BasememoryService;
import com.fps.web.service.MminfoService;
import com.fpswork.Helper.HttpResult;
import com.fpswork.core.model.ExcelEntity;
import com.fpswork.core.model.PageEntity;
import com.fpswork.util.ExcelUtil;
import com.fpswork.util.StringUtil;


@Controller
@RequestMapping("/openInterface")
public class OpenInterfaceController{
    @Autowired
    BasememoryService basememoryService;
    
    
    @Autowired
    BaseService baseService;
    @Autowired
    MminfoService mminfoService;
		/*
	    *实例
	    */
	    @RequestMapping(value = "/eg")
	    public  @ResponseBody HttpResult eg(Model model,String date,HttpServletResponse response) { 
	    	try{	
				return HttpResult.success(model);
			}catch(Exception e){
				return HttpResult.build("error", e.getMessage());
			}
	    	
	    }

	    
	    @RequestMapping("/{requestName}")
	    public ModelAndView translate(@PathVariable String requestName,String responsePath) throws Exception{
	       	ModelAndView modelAndView = new ModelAndView();   
	       	modelAndView.setViewName(responsePath);   
			return modelAndView;
	    }
	    /**
		 * 加载List数据
		 */
	    @RequestMapping(value = "/loadMminfoList", method = RequestMethod.GET)
	    public @ResponseBody PageEntity loadMminfoList(PageEntity pageEntity) throws Exception{
	    	mminfoService.queryMminfoList(pageEntity);
	        return pageEntity;
	    }
	    
	    
	    
	    /**
	   	 * 陌梦基础信息管理
	   	 */
	    @RequestMapping("/mminfoManagement")
	    public ModelAndView mminfoManagement(String id,String pagetype) throws Exception{
	    	ModelAndView modelAndView = new ModelAndView();   
	    	Mminfo mminfo= new Mminfo();
	    	if(!"add".equals(pagetype)){
	    		mminfo = mminfoService.queryMminfoByID(id);
	    	}
	    	modelAndView.addObject("mminfo",mminfo);
	    	modelAndView.addObject("pagetype", pagetype);
			modelAndView.setViewName("web/openInterface/mminfomanagement");      
	        return modelAndView;
	    }
	    
	    /**
	   	 * 陌梦基础信息管理新增表单
	   	 */
	    @RequestMapping(value = "/mminfoAddSubmit", method = RequestMethod.POST)
	    public @ResponseBody Map<String, String> mminfoAddSubmit(@ModelAttribute("Mminfo")Mminfo mminfo) throws Exception{
	    	mminfoService.addMminfo(mminfo);
	    	Map<String, String> map = new HashMap<String, String>();
	    	map.put("flag", "true");
	        return map;
	    }
	     
	    /**
	   	 * 陌梦基础信息管理修改表单
	   	 */
	    @RequestMapping(value = "/mminfoUpdateSubmit", method = RequestMethod.POST)
	    public @ResponseBody Mminfo mminfoUpdateSubmit(@ModelAttribute("Mminfo")Mminfo mminfo) throws Exception{
	    	mminfoService.updateMminfo(mminfo);
	        return mminfo;
	    }
	    
	    
	    /**
	   	 * 陌梦基础信息删除
	   	 */
	    @RequestMapping(value = "/deleteMminfo", method = RequestMethod.POST)
	    public @ResponseBody Map<String, String> deleteMminfo(@ModelAttribute("Mminfo")Mminfo mminfo) throws Exception{
	    	mminfoService.deleteMminfo(mminfo);
	    	Map<String, String> map = new HashMap<String, String>();
			map.put("flag", "true");
	        return map;
	    }
	    
		/**
	   	 * DownExcel
	   	 * 下载陌梦基础信息excel
	   	 */
	    @RequestMapping(value = "/DownExeclMminfo", method = RequestMethod.GET)
	    public @ResponseBody Map<String, String> DownExeclMminfo(HttpServletResponse res,HttpServletRequest req,String exceldata) throws Exception{
	       Map<String, String> map = new HashMap<String, String>();
	       	ExcelEntity excelEntity = new ExcelEntity();
	       	//存放对应的表头名称和对应的数据库字段
	    	Map<String, Object> titlemap = new LinkedHashMap<String, Object>();
	    	//下载文件名
	    	String fileName=".xls";
	       	if(exceldata.contains("||") ){// 以"||"分割的字符串
		    	  List<String> arg=  StringUtil.splitString(exceldata, "||");
		    	  for(int i=0;i<arg.size();i++){
		    		  if(i==0){
		    			  fileName=arg.get(0)+fileName; 
		    		  }else{
		    			  String[] arg1=   arg.get(i).split(":"); 
		    			  titlemap.put(arg1[0], arg1[1]);
		    		  }
		    	  }
		    }
	       
	    	excelEntity.setFilename(fileName);
	    	excelEntity.setLinename(titlemap);
	    	//直接查询结果集的语句
	    	String sql="select * from Mminfo where 1=1";
	    	excelEntity.setSql(sql);
	    	this.DownExecl(res, req, excelEntity);
	    	map.put("flag", "true");
	        return map;
	    }
	    public void DownExecl(HttpServletResponse res,HttpServletRequest req,ExcelEntity excelEntity) throws Exception{
	    	baseService.DownExecl(excelEntity);
	       	ExcelUtil.DownExecl(res, req,excelEntity);
	    }
}
